
package com.huawei.saashousekeeper.interceptor;

import com.huawei.saashousekeeper.config.TenantContext;

import org.springframework.core.Ordered;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 租户标识过滤器
 *
 * @author lWX1128557
 * @since 2022-04-01
 */
public class TenantDomainFilter extends OncePerRequestFilter implements Ordered {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
        String schema = request.getHeader("tenantDomain");
        TenantContext.set(schema);
        try {
            filterChain.doFilter(request, response);
        } finally {
            TenantContext.remove();
        }
    }

    @Override
    public int getOrder() {
        return -100;
    }
}
